import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test2/base/base_page.dart';
import 'package:test2/util/widget.dart';

import '../style/style.dart';
import '../util/sp.dart';

class TagSetting extends StatefulWidget {
  const TagSetting({super.key});

  @override
  State<TagSetting> createState() => _TagSettingState();
}

class _TagSettingState extends BaseState<TagSetting> {
  List<String> customTags = [];
  TextEditingController _controller = TextEditingController();
  bool changed = false;

  @override
  void initState() {
    init();
    super.initState();
  }

  init() async {
    setState(() {
      customTags.addAll(SpUtil.get().getStringList("tags") ?? []);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("标签管理")),
      body: SizedBox.expand(
        child: Container(
          padding: EdgeInsets.all(16),
          child: Column(
            spacing: 8,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text("默认："),
              _tags(defaultTags),
              SizedBox(height: 12),

              Row(
                children: [
                  Text("自定义："),
                  if (changed)
                    Text("未保存", style: TextStyle(color: Colors.pink)),
                ],
              ),
              Row(
                children: [
                  Expanded(
                    child: TextField(
                      controller: _controller,
                      decoration: InputDecoration(label: Text("标签名")),
                    ),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      addTag();
                    },
                    child: Text("新增"),
                  ),
                ],
              ),
              _tags(customTags, edit: true),
              Spacer(),
              SizedBox(
                width: double.maxFinite,
                child: ElevatedButton(
                  onPressed: () {
                    save();
                  },
                  child: Text("保存"),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  _tags(List<String> tags, {bool edit = false}) {
    return Wrap(
      spacing: 12,
      runSpacing: 8,
      children: List.generate(tags.length, (index) {
        return BorderContainer(
          radius: 30,
          bgColor: Theme.of(context).primaryColorLight,
          child: Padding(
            padding: EdgeInsetsGeometry.symmetric(vertical: 4, horizontal: 12),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              spacing: 4,
              children: [
                Text(tags[index]),
                if (edit)
                  GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: () {
                      setState(() {
                        tags.removeAt(index);
                        changed = true;
                      });
                    },
                    child: Icon(Icons.clear, size: 16),
                  ),
              ],
            ),
          ),
        );
      }),
    );
  }

  addTag() {
    if (_controller.text.isEmpty) {
      showToast("请输入标签名");
      return;
    }

    setState(() {
      customTags.add(_controller.text);
      changed = true;
    });

    _controller.text = "";
  }

  save() {
    SpUtil.get().setStringList("tags", customTags);
    setState(() {
      changed = false;
    });
    showToast("保存成功");
  }
}
